$(document).ready(function(){
	if(getParam("picture")!=""){
		cutp();
		}
	
		
	if(getParam("avatar")!=""){
		$("#edit_info").hide();
		$("#edit_pwd").hide();
		$("#edit_avatar").show(300);
		$('.user_menu li a').removeClass('title_current');
		$('#avatar_link').addClass('title_current');
		}

	$("#pwd_link").click(function(){
		$("#edit_info").hide();
		$("#edit_avatar").hide();
		$("#edit_pwd").show(300);
		$('.user_menu li a').removeClass('title_current');
		$('#pwd_link').addClass('title_current');
		return false;
		});
		
	$("#info_link").click(function(){
		$("#edit_pwd").hide();
		$("#edit_avatar").hide();
		$("#edit_info").show(300);
		$('.user_menu li a').removeClass('title_current');
		$('#info_link').addClass('title_current');
		return false;
		});
		
	$("#avatar_link").click(function(){
		$("#edit_info").hide();
		$("#edit_pwd").hide();
		$("#edit_avatar").show(300);
		$('.user_menu li a').removeClass('title_current');
		$('#avatar_link').addClass('title_current');
		return false;
		});
		
	$("#submit_pwd").click(function(){
		if($("#pwd_old").val()=="" || $("#pwd_new").val()==""){$("#pwd_message").html("<div class='edit_save'><div class='fail'></div>密码均不能为空</div>");}
		else if($("#pwd_new").val()!=$("#pwd_new1").val()){$("#pwd_message").html("<div class='edit_save'><div class='fail'></div>两次输入密码不一致</div>");}
		else{
			$.ajax({url:"edit_user_info.class.php?request=pwd&pwd_old="+$("#pwd_old").val()+"&pwd_new="+$("#pwd_new").val()+"",async:false,success:function(result){
				if(result=="true"){$("#pwd_message").html("<div class='edit_save'><div class='succed'></div> 成功修改了密码</div>");
					setTimeout(function(){
						location.href="edit_user_info.php";
					},1000);}
				else if(result=="error password"){$("#pwd_message").html("<div class='edit_save'><div class='fail'></div>密码错误</div>");}
				}});
			}
		});
		
	$("#submit_main").click(function(){

		if($("#user_name").val()!="" && Checkval("user_name") <= 12){
			$.ajax({url:"edit_user_info.class.php?request=main&user_name="+$("#user_name").val()+"&true_name="+$("#true_name").val()+"&sex="+$("#selSex").val()+"&year="+$("#selYear").val()+"&month="+$("#selMonth").val()+"&day="+$("#selDate").val()+"&province="+$("#province").val()+"&city="+$("#city").val()+"&remark="+$("#remark").val()+"",async:false,success:function(result){
				if(result="true"){
					$("#main_message").html("<div class='edit_save'><div class='succed'></div> 成功保存了帐号设置</div>");
					setTimeout(function(){
						location.href="edit_user_info.php";
					},1000);
				}}});
			}else{$("#main_message").html("<div class='edit_save'><div class='fail'></div>请至少填写昵称且不能超过6个中文字符</div>");}
			return false;
		});
		
	$("#submit_avatar").click(function(){
		$.ajax({url:"edit_user_info.class.php?request=avatar&src="+$("#ajax_avatar").attr("src")+"",async:false,success:function(result){
			if(result=="true"){$("#avatar_message").html("<div class='edit_save'><div class='succed'></div>成功修改了头像</div>");
				setTimeout(function(){
						location.href="edit_user_info.php";
					},1000);
				}
			}});
		});
	
	$("#buttom_upload").click(function(){
		cli("photo");
		return false;
		});
		
	$("#photo").change(function(){
		cli("p_submit");
		});
	
	
	function cli(id){  
		var sub = document.getElementById(id);
   	sub.click();
//   	if (/msie/i.test(navigator.userAgent)) //IE  
//   {  
//      sub.fireEvent("onclick");  
//   } else {  
//      var e = document.createEvent('MouseEvent');  
//      e.initEvent('click', false, false);  
//      sub.dispatchEvent(e);  
//   }  
	}
	
	function Checkval(did)   
    {   
      var s=document.getElementById(did).value;   
      var n=0;   
      for(var i=0;i<s.length;i++)   
      {   
      //charCodeAt()可以返回指定位置的unicode编码,这个返回值是0-65535之间的整数   
        if(s.charCodeAt(i)<128)   
        {   
         n++;    
        }   
        else   
        {   
         n+=2;   
        }   
      }   
      return n;   
    }
	
	function getParam(paramName){
   			 paramValue = "";
   			 isFound = false;
   			 if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=")>1){
   			     arrSource = unescape(this.location.search).substring(1,this.location.search.length).split("&");
   			     i = 0;
   			     while (i < arrSource.length && !isFound){
   			         if (arrSource[i].indexOf("=") > 0){
   			              if (arrSource[i].split("=")[0].toLowerCase()==paramName.toLowerCase()){
   			                 paramValue = arrSource[i].split("=")[1];
   			                 isFound = true;
   			              }
   			         }
   			         i++;
   			     }   
   			 }
   			return paramValue;
			}
			
			function updateCoords(c){
				$('#x').val(c.x);
				$('#y').val(c.y);
				$('#w').val(c.w);
				$('#h').val(c.h);
			}

			function checkCoords(){
				if (parseInt($('#w').val())) return true;
				alert('请选择一个区域');
				return false;
			}
		
		/* 城市选择器 */
	cities = new Array();
	pro = new Array("北京","上海","天津","重庆", "香港特别行政区","澳门特别行政区", "河北省", "山西省", "辽宁省", "吉林省", "江苏省", "浙江省", "安徽省", "福建省", "江西省", "山东省", "河南省", "湖北省", "湖南省", "广东省", "广西壮族自治区", "海南省", "四川省", "贵州省", "云南省", "陕西省", "甘肃省", "青海省", "宁夏回族自治区", "新疆维吾尔自治区", "内蒙古", "黑龙江省","西藏自治区","台湾省","海外");
	cities['北京'] = new Array("北京");
	cities['上海'] = new Array("上海");
	cities['重庆'] = new Array("重庆");
	cities['香港特别行政区'] = new Array("香港特别行政区");
	cities['澳门特别行政区'] = new Array("澳门特别行政区");
	cities['河北省'] = new Array("唐山", "邯郸", "保定", "承德", "秦皇岛", "石家庄");
	cities['山西省'] = new Array("太原", "大同", "阳泉", "长治", "晋城", "晋中", "运城", "临汾");
	cities['内蒙古'] = new Array("呼和浩特", "呼伦贝尔", "包头", "乌海", "赤峰", "兴安盟");
	cities['辽宁省'] = new Array("沈阳", "大连", "鞍山", "抚顺", "本溪", "丹东", "锦州", "阜新", "辽阳", "铁岭");
	cities['吉林省'] = new Array("长春", "吉林", "四平", "辽源", "通化", "松原");
	cities['黑龙江省'] = new Array("哈尔滨", "大兴安岭", "鸡西", "大庆", "黑河", "齐齐哈尔");
	cities['江苏省'] = new Array("南京", "无锡", "徐州", "常州", "苏州", "南通", "扬州", "镇江", "连云港");
	cities['浙江省'] = new Array("杭州", "宁波", "温州", "嘉兴", "绍兴", "金华", "衢州", "台州", "丽水");
	cities['安徽省'] = new Array("合肥", "芜湖", "蚌埠", "淮南", "淮北", "铜陵", "安庆", "黄山", "马鞍山");
	cities['福建省'] = new Array("福州", "厦门", "莆田", "三明", "泉州");
	cities['江西省'] = new Array("南昌", "九江", "鹰潭", "赣州", "上饶", "景德镇");
	cities['山东省'] = new Array("济南", "青岛", "烟台", "德州", "聊城", "滨州", "荷泽");
	cities['河南省'] = new Array("郑州", "开封", "洛阳", "安阳", "许昌", "南阳", "周口", "驻马店", "三门峡");
	cities['湖北省'] = new Array("武汉", "黄石", "宜昌", "荆门", "荆州", "黄冈", "咸宁", "恩施");
	cities['湖南省'] = new Array("长沙", "湘潭", "岳阳", "常德", "益阳", "郴州", "湘西", "张家界");
	cities['广东省'] = new Array("广州", "韶关", "深圳", "珠海", "汕头", "佛山", "江门", "湛江", "惠州", "东莞", "中山");
	cities['广西壮族自治区'] = new Array("南宁", "柳州", "桂林", "北海", "玉林");
	cities['海南省'] = new Array("海口", "三亚", "海南沿革");
	cities['四川省'] = new Array("成都", "泸州", "德阳", "绵阳", "乐山", "眉山", "宜宾", "攀枝花");
	cities['贵州省'] = new Array("贵阳", "遵义", "安顺", "铜仁", "六盘水");
	cities['云南省'] = new Array("昆明", "曲靖", "玉溪", "保山", "丽江", "大理");
	cities['陕西省'] = new Array("西安", "铜川", "宝鸡", "咸阳", "延安", "汉中");
	cities['甘肃省'] = new Array("兰州", "金昌", "白银", "天水", "嘉峪关");
	cities['青海省'] = new Array("西宁", "海东", "海北", "黄南", "海南", "果洛", "玉树", "海西");
	cities['宁夏回族自治区'] = new Array("银川", "吴忠", "固原", "中卫");
	cities['新疆维吾尔自治区'] = new Array("乌鲁木齐", "克拉玛依", "哈密", "伊犁", "吐鲁番");
	cities['西藏自治区'] = new Array("拉萨", "昌都地区", "林芝地区", "山南地区", "日喀则地区","那曲地区","阿里地区");
	cities['台湾省'] = new Array("台北", "台中", "高雄", "台南", "新北","基隆","新竹","嘉义");
	cities['海外'] = new Array("海外");
	
	$("select#province").change(function(){
		var i = $('#province option:selected').val();
		$('#city').empty();
		for (c in cities[i]) {
			$("<option>" + cities[i][c] + "</option>").appendTo('select#city');
		}
	});

		
			
      $('#jc-hidden-image').Jcrop({
      	aspectRatio: 1,
				onSelect: updateCoords,
				setSelect: [60,70,340,330]
      	});
			
      function cutp(){
      	//alert(imgsrc);
        var jcrop_api;
        var $dialog = $('<div><div class="jc-dialog"><img src="'+$("#jc-hidden-image").attr("src")+'"/></div></div>');
        $dialog.find('img').Jcrop({aspectRatio: 1,
				onSelect: updateCoords,
				setSelect: [80,70,240,330]
				},function(){
          jcrop_api = this;
          $dialog.dialog({
            modal: true,
            title: '请选择一个区域作为头相',
            close: function(){ $dialog.remove(); },
            width: jcrop_api.getBounds()[0]+34,
            resizable: false
          })
        });
        $dialog.append("<form action='' method='post' onsubmit='return checkCoords();'><input type='hidden' id='x' name='x' /><input type='hidden' id='y' name='y' /><input type='hidden' id='w' name='w' /><input type='hidden' id='h' name='h' /><input type='submit' value='预览' name='submit'/></form>");
        return false;
     	}

});
